package com.zhl.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhl.model.User;
import com.zhl.service.UserService;
import com.zhl.util.SpringApplicationContext;

@Controller
@RequestMapping("/hello")
public class SpringController {
	
	@Autowired
	UserService UserService;
	//@Autowired
	//SpringApplicationContext springApplicationContext;
	//ApplicationContext context = new ClassPathXmlApplicationContext( "config/spring-common.xml");
	//UserService userService = (UserService) context.getBean("userService"); 
	//UserService userService = (UserService) springApplicationContext.getBean("userService"); 
	@RequestMapping(method = RequestMethod.GET)
	   public String printHello(ModelMap model) {
		  System.out.println("Hello World！");
	      model.addAttribute("message", "Hello Spring MVC Framework!");
	      return "helloworld";
	   }
	
	@Cacheable(value="userCache")
	@RequestMapping(value="user",method = RequestMethod.GET)
	   public String user(HttpServletRequest req,ModelMap model) {
		 
		  String name = req.getParameter("name");
		  if(name == null || name.equals("")){
			  return "helloworld";
		  }
		 
		  UserService.getUserByName(name);
	      model.addAttribute("user", UserService.getUserByName(name));
	      model.addAttribute("user", UserService.getUserByName(name));
	      model.addAttribute("user", UserService.getUserByName(name));
	      return "user";
	   }

	@RequestMapping(value="users",method = RequestMethod.GET)
	   public String users(ModelMap model) {
		  //System.out.println("test！");
		  
		  List<User> users = UserService.getAll();
		  
		  model.addAttribute("users", users);

	      return "users";
	   }

}
